package com.zhw.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zhw.Util.JwtUtils;
import com.zhw.mapper.ChanceMapper;
import com.zhw.mapper.UserMapper;
import com.zhw.pojo.dto.ChanceQueryDTO;
import com.zhw.pojo.PageBean;
import com.zhw.pojo.entry.SaleChance;
import com.zhw.pojo.entry.User;
import com.zhw.service.ChanceService;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
@Slf4j
public class ChanceServiceImpl implements ChanceService {

    @Autowired
    ChanceMapper chanceMapper;


    @Autowired
    UserMapper userMapper;

    @Autowired
    HttpServletRequest request;

    @Override
    public PageBean page(ChanceQueryDTO chanceQueryDTO) {
        PageHelper.startPage(chanceQueryDTO.getPage(),chanceQueryDTO.getPageSize());
        Page<SaleChance> page=chanceMapper.page(chanceQueryDTO);
        return new PageBean(page.getTotal(),page.getResult());

    }

    @Override
    public void save(SaleChance saleChance) {
        Integer id=chanceMapper.getIdByUsername(saleChance.getAssignMan());
        saleChance.setAssignMan(String.valueOf(id));

        String token = request.getHeader("token");
        Claims claims = JwtUtils.parseJWT(token);
        saleChance.setCreateMan((String) claims.get("username"));

        saleChance.setStatus(0);
        saleChance.setDevResult(0);
        saleChance.setIsValid(0);

        saleChance.setCreateDate(LocalDateTime.now());
        saleChance.setUpdateDate(LocalDateTime.now());

        if (saleChance.getAssignMan()!=null){
            saleChance.setAssignTime(LocalDateTime.now());
            saleChance.setStatus(1);
            saleChance.setDevResult(1);
        }


        log.info("最终添加的营销信息：{}",saleChance);
        chanceMapper.save(saleChance);


    }


    @Override
    public SaleChance getByID(Integer id) {
        SaleChance saleChance = chanceMapper.getByID(id);
        if (!saleChance.getAssignMan().isEmpty()) {
            User u = userMapper.getByID(Long.valueOf(saleChance.getAssignMan()));
            saleChance.setAssignMan(u.getUsername());
        }
        return saleChance;
    }


    @Override
    public void update(SaleChance saleChance) {
        saleChance.setUpdateDate(LocalDateTime.now());

        if (!saleChance.getAssignMan().isEmpty()){
            User user=userMapper.getByUsername(saleChance.getAssignMan());
            saleChance.setAssignMan(String.valueOf(user.getId()));
        }
        chanceMapper.update(saleChance);
    }


    @Override
    public void deleteByID(Integer id) {
        chanceMapper.deleteByID(id);
    }


    @Override
    public void updateResult(SaleChance saleChance) {
        saleChance.setUpdateDate(LocalDateTime.now());
        saleChance.setDevResult(saleChance.getIsValid()==1?2:3);
        chanceMapper.update(saleChance);
    }
}
